package han.bashe.web.servlet;

import han.bashe.business.ejb.LogQuery;
import han.bashe.common.entity.BasheEntityConstants;
import han.bashe.common.entity.SoaConversation;
import han.bashe.common.entity.SoaMessageExchange;
import han.bashe.common.entity.SoaMessageLog;
import han.bashe.common.exception.BasheErrorCode;
import han.bashe.common.exception.BasheException;
import han.bashe.common.util.ExceptionUtils;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.xmlbeans.XmlObject;

import v1.getResult.service.innner.bashe.han.GetResultServiceRequestDocument;
import v1.getResult.service.innner.bashe.han.GetResultServiceResponseDocument;
import v1.message.bashe.han.BasheErrorDocument;

/**
 * Servlet implementation class for Servlet: GetResultServlet
 *
 */
 public class GetResultServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
   @EJB
   private LogQuery query;
   
    /* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public GetResultServlet() {
		super();
	}   	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}  	
	
	/* (non-Java-doc)
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try{
			String reqXml = ServletReqResHeler.getRequestString(request);
			GetResultServiceRequestDocument doc = GetResultServiceRequestDocument.Factory.parse(reqXml);
			if(! doc.validate()){
				throw new BasheException(BasheErrorCode.PARSE_CLIENT_XML_ERROR,"request xml invalid");
			}
			GetResultServiceResponseDocument resDoc = GetResultServiceResponseDocument.Factory.newInstance();
			resDoc.addNewGetResultServiceResponse();
			String id = doc.getGetResultServiceRequest().getConversationId();
			SoaConversation con = query.getConversation(id);
			
			if(con == null){
				throw new BasheException(BasheErrorCode.CONVERSATION_NOT_EXISTS_ERROR,"no such conversation" +
						" id [" + id +
						"]");
			}
			if(con.getStatus().equals(BasheEntityConstants.STATUS_ERROR)){
				throw new BasheException(BasheErrorCode.CONVERSATION_STATUS_ERROR,"conversation" +
						" id [" + id +
						"]'s status is STATUS_ERROR, which means error occurred, please contact system admin");
			}
			
			if(con.getResponseLogId() != null){
				SoaMessageExchange me = query.getMessageExchangeById(con.getResponseLogId());
				XmlObject xo = XmlObject.Factory.parse(me.getRequest().getMessage());
				resDoc.getGetResultServiceResponse().set(xo);
			}
			
			ServletReqResHeler.setResponseString(response, resDoc.toString());
		}catch (BasheException e) {
			Log log = LogFactory.getLog(HttpServlet.class);
			log.error("GetResultServlet.doPost() error", e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(e.getCode());
			error.getBasheError().setErrorMsg(e.getMsg());
			error.getBasheError().setErrorTrace(
					ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());

		} catch (Exception e) {
			Log log = LogFactory.getLog(LogQueryServlet.class);
			log.error("GetResultServlet.doPost() error", e);
			BasheErrorDocument error = BasheErrorDocument.Factory.newInstance();
			error.addNewBasheError();
			error.getBasheError().setErrorCode(BasheErrorCode.UNKNOWN_ERROR);
			error.getBasheError().setErrorMsg(e.getMessage());
			error.getBasheError().setErrorTrace(
					ExceptionUtils.getTraceAsString(e));
			ServletReqResHeler.setResponseString(response, error.toString());
		}
	}   	  	    
}